package com.eqdd.yiqidian.kotlin

import android.support.v7.widget.LinearLayoutManager
import com.alipay.sdk.auth.AlipaySDK
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.bean.SpaceVisitorBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import java.util.*

/**
 * Created by jhj on 18-6-27.
 */
class Test : BaseActivity() {

    private var mvcHelper: MVCCoolHelper<List<SpaceVisitorBean>>? = null
    private var pageNum: Int = 0
    private var slimAdapterEx: SlimAdapterEx<SpaceVisitorBean>? = null
    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
    }

    override fun initData() {

        val list = arrayListOf<SpaceVisitorBean>()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        slimAdapterEx = SlimAdapterEx.create().register<SpaceVisitorBean>(R.layout.list_item_155, SlimInjector<SpaceVisitorBean> { data, injector ->
            injector?.text(R.id.tv_name,data?.staffName)
                    ?.text(R.id.tv_depart,String.format(Locale.CHINA, "%s - %s", data?.departName, data?.postName))
                    ?.imageCircle(R.id.iv_photo,HttpConfig.BASE_URL + data?.iphoto)
        }).attachTo(recyclerView).updateData(list as List<SpaceVisitorBean>?) as SlimAdapterEx<SpaceVisitorBean>?

        val dataSource = ModelRx2DataSource<SpaceVisitorBean>(ModelRx2DataSource.OnLoadSource<SpaceVisitorBean> { page, register ->
            OkGo.post<HttpResult<List<SpaceVisitorBean>>>(HttpConfig.BASE_URL + HttpConfig.SPACE_VISITOR_LIST)
                    .params("companyId", user.companyId)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResult<List<SpaceVisitorBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    }).adapt(FlowableBody<HttpResult<List<SpaceVisitorBean>>>())
                    .flatMap({ result ->
                        if (page == 1) {
                            mvcHelper?.setAutoLoadMore(true)
                        }
                        pageNum = result.getNextpage()
                        Flowable.just<MutableList<SpaceVisitorBean>>(if (result.items == null) ArrayList() else result.items as MutableList<SpaceVisitorBean>?)
                    })
        }, 10)

        mvcHelper = MVCCoolHelper<List<SpaceVisitorBean>>(coolRefreshView)
        mvcHelper?.setDataSource(dataSource)
        mvcHelper?.adapter = slimAdapterEx
        mvcHelper?.refresh()
    }

    override fun setView() {

    }


}